import datetime
import time

import pyautogui
import pyperclip

import Config
from driver.driver import get_control_by_level, get_control_position, mouse_move_to_and_click, clear_and_send_keys
from public.public import get_soldier_cities, get_solider_cities_reserve_config, entry_city, get_people_num, \
    get_food_num, get_wood_num, get_mine_num, get_iron_num, open_map, get_translate_city, get_resource_points, \
    chose_city, get_commander_city, get_remaining_resource_num
from 征兵.main import auto_make_soldiers
from 运输.main import translate_by_solider_type, send_resource


class autoMakeSoldier:
    control = None
    left = None
    top = None
    width = None
    height = None

    def __init__(self, control):
        self.control = control
        self.left, self.top, self.width, self.height = get_control_position(control)

    def get_last_solider_city(self):
        """
        获取最后一个兵城
        :param control:
        :return:
        """
        soldier_cities = get_soldier_cities(self.control)
        if get_solider_cities_reserve_config(self.control):
            soldier_cities.reverse()
        return soldier_cities[-1]

    def entry_last_solider_city(self):
        """
        进入最后一个兵城
        :return:
        """
        city = self.get_last_solider_city()
        entry_city(self.left, self.top, city[0], city[1], cityin=False)

    def get_people_num(self):
        """
        获取当前城池的人口数量
        :return:
        """
        return get_people_num(self.control)

    def get_food_num(self):
        """
        获取当前城池的食物数量
        :return:
        """
        return get_food_num(self.control)

    def get_wood_num(self):
        """
        获取当前城池的木材数量
        :return:
        """
        return get_wood_num(self.control)

    def get_mine_num(self):
        """
        获取当前城池的石料数量
        :return:
        """
        return get_mine_num(self.control)

    def get_iron_num(self):
        """
        获取当前的钢铁数量
        :return:
        """
        return get_iron_num(self.control)

    def get_resource_city(self):
        """
        获取当前资源城坐标
        :return:
        """
        return get_translate_city(self.control)

    def entry_resource_city(self):
        """
        进入资源城池
        :return:
        """
        _ = self.get_resource_city()
        entry_city(self.left, self.top, _[0], _[1], cityin=False)

    def callback_resource(self):
        """
        召回资源
        :return:
        """
        resource_points = get_resource_points(self.control)
        for resource_point in resource_points:
            chose_city(self.left, self.top, resource_point[0], resource_point[1])

            _ = Config.DiTuConfig.CallBackButton
            mouse_move_to_and_click(self.left + _[0], self.top + _[1])

    def entry_commander_city(self):
        """
        进入将军城池
        :return:
        """
        _ = get_commander_city(self.control)
        entry_city(self.left, self.top, _[0], _[1], cityin=False)

    def send_resource(self, translate_type=Config.SoliderType.ZiZhong, translate_num=10000):
        """
        派遣资源
        :return:
        """

        def check_resource_num(resource_num):
            pyautogui.hotkey("ctrl", "a")
            time.sleep(0.1)
            pyautogui.hotkey("ctrl", "c")
            paste = pyperclip.paste()
            if str(resource_num).strip() == str(paste).strip():
                time.sleep(1)
                return True
            else:
                remaining_resource_num = get_remaining_resource_num(self.control)
                if remaining_resource_num == 0:
                    return True
                return False

        def check_resource_num2(resource_num):
            remaining_resource_num = get_remaining_resource_num(self.control)
            if remaining_resource_num != 0:
                pyautogui.hotkey("ctrl", "a")
                time.sleep(0.1)
                pyautogui.hotkey("ctrl", "c")
                paste = pyperclip.paste()
                clear_and_send_keys(int(paste)-800*10000,check=True)
            return True


        resource_points = get_resource_points(self.control)
        send_count = 0
        max_resource = 14*10000*10000
        while True:
            send_count += 1
            send_count %= len(resource_points)
            resource_point = resource_points[send_count]
            chose_city(self.left, self.top, resource_point[0], resource_point[1])

            _ = Config.DiTuConfig.SendButton
            mouse_move_to_and_click(self.left + _[0], self.top + _[1])

            _ = translate_type["jiaochang"]
            mouse_move_to_and_click(self.left + _[0], self.top + _[1])
            clear_and_send_keys(translate_num)
            time.sleep(1)

            # 派遣铁锭
            iron_num = get_iron_num(self.control)
            if iron_num > 100*10000:
                _ = Config.JiaoChang.IronText
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                clear_and_send_keys(max_resource, check_resource_num)

                _ = Config.JiaoChang.ChuZheng
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])

                # 关闭校场界面
                time.sleep(1)
                _ = Config.JiaoChang.CloseButton1
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                continue

            # 派遣木材
            wood_num = get_wood_num(self.control)
            if wood_num > 100*10000:
                _ = Config.JiaoChang.WoodText
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                clear_and_send_keys(max_resource, check_resource_num)

                _ = Config.JiaoChang.ChuZheng
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                # 关闭校场界面
                time.sleep(1)
                _ = Config.JiaoChang.CloseButton1
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                continue

            # 派遣粮食
            food_num = get_food_num(self.control)
            if food_num > 1000*10000:
                _ = Config.JiaoChang.FoodText
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                clear_and_send_keys(max_resource, check_resource_num2)

                _ = Config.JiaoChang.ChuZheng
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                # 关闭校场界面
                time.sleep(1)
                _ = Config.JiaoChang.CloseButton1
                mouse_move_to_and_click(self.left + _[0], self.top + _[1])
                continue


def get_minute():
    """
    获取当前时间的分钟数
    :return:
    """
    now = datetime.datetime.now()
    return now.minute


def main(
        solider_type=Config.SoliderType.QingQi,
        solider_num=1000,
        double=True,
        translate_type=Config.SoliderType.ZiZhong,
        translate_num=10000,
):
    # 程序开始
    while True:
        # 获取当前的分钟数
        minute = get_minute()
        # 如果当前分钟数不是前20分钟，则退出睡眠一分钟，退出当前循环
        if minute > 20:
            time.sleep(60)
            continue

        # 判断当前网络是否出现异常，如果出现异常则退出程序
        # 先不写，由手动退出程序

        # 获取当前窗口对象
        control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
        left, top, width, height = get_control_position(control)

        # 第一步：判断当前的人口资源是否充足
        # 初始化自动招兵对象
        auto_make_soldier = autoMakeSoldier(control)
        # 进入最后一个兵城
        auto_make_soldier.entry_last_solider_city()
        # 获取当前城池的人数
        people_num = auto_make_soldier.get_people_num()
        # 计算招募的兵种所需人口数量是否小于当前城池的人口数量
        if people_num < solider_type["resource"]["People"] * solider_num * 12:
            time.sleep(60)
            continue

        # 第二步： 进入资源城池，并召回资源
        auto_make_soldier.entry_resource_city()
        open_map(left, top)
        auto_make_soldier.callback_resource()

        # 循环判断当前的资源已召回，否则睡眠一分钟
        while True:
            # 先判断粮食是否召回
            food_num = auto_make_soldier.get_food_num()
            if food_num < solider_type["resource"]["Food"] * solider_num * 12 + 100000:
                time.sleep(60)
            break
        while True:
            # 判断木材是否召回
            food_num = auto_make_soldier.get_wood_num()
            if food_num < solider_type["resource"]["Wood"] * solider_num * 12 + 100000:
                time.sleep(60)
            break
        while True:
            # 判断石料是否召回
            food_num = auto_make_soldier.get_mine_num()
            if food_num < solider_type["resource"]["Mine"] * solider_num * 12 + 100000:
                time.sleep(60)
            break
        while True:
            # 判断钢铁是否召回
            food_num = auto_make_soldier.get_iron_num()
            if food_num < solider_type["resource"]["Iron"] * solider_num * 12 + 100000:
                time.sleep(60)
            break

        # 第三步： 开始运输资源
        translate_by_solider_type(solider_type["resource"], solider_num, num=10000, double=double)

        # 第四步：开始招兵
        # 进入当前招兵城池
        auto_make_soldier.entry_commander_city()
        auto_make_soldiers(soldier_type=solider_type["barrack"], num=solider_num, double=double)

        # 第五步： 将资源派遣到资源节点
        auto_make_soldier.entry_resource_city()
        open_map(left, top)
        auto_make_soldier.send_resource(translate_type=translate_type, translate_num=translate_num)


if __name__ == '__main__':
    # main()
    # get_minute()
    time.sleep(10)
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    # 初始化自动招兵对象
    auto_make_soldier = autoMakeSoldier(control)
    # 进入最后一个兵城
    auto_make_soldier.entry_last_solider_city()
    # 获取当前城池的人数
    people_num = auto_make_soldier.get_people_num()
    print(people_num)
